package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.UserDao;
import com.daoimpl.UserDaoImpl;
import com.vo.User;

import java.io.IOException;

@WebServlet("/ForgetPasswordCheckServlet")
public class ForgetPasswordCheckServlet extends HttpServlet {
	
	  @Override
	    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	        super.doGet(req, resp);
	    }

	    @Override
	    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        request.setCharacterEncoding("UTF-8");
	        response.setHeader("content-type", "text/html; charset=UTF-8");
	        
	        User user=new User();
	        user.setUsername(request.getParameter("username"));
	        user.setQuestion(request.getParameter("question"));
	        user.setAnswer(request.getParameter("answer"));
	      
	        UserDao dao=new UserDaoImpl();
	        user = dao.checkUserForget(user);
	        if(user != null) {
	        	HttpSession session = request.getSession();
	        	session.setAttribute("userinfo", user);
	        	request.getRequestDispatcher("forgetPassword.jsp").forward(request, response);
	        }else{
	        	response.getWriter().println("用户名或密保不正确，请重新输入！");
	        	response.setHeader("Refresh", "0.5; URL=forget.jsp");
	        }
	    }
}
